package com.oasistudio.ads.expert.banner;

import android.app.Activity;
import android.view.View;
import android.widget.LinearLayout;

import com.oasistudio.ads.expert.R;
import com.taobao.munion.ads.clientSDK.TaoAds;
import com.taobao.munion.ads.clientSDK.TaoAdsListener;

public class AdViewTaobao {
	TaobaoAdRequestListener adRequestListener;
	Activity activity;
	LinearLayout taobaoContainer;
	public static final String UNION = "taobao.com";
	public AdViewTaobao(Activity activity, String pid){
		System.out.println(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,AdViewTaobao() pid = "+pid);
		this.activity = activity;
		this.adRequestListener = new TaobaoAdRequestListener(activity, UNION);
		addTaobaoAd(pid);
	}
	
	private TaoAds ad;
	public TaoAds getTaoAds(){
		return this.ad;
	}
	
	/************************ munion.taobao.com start ***********************/
	private void addTaobaoAd(String pid) {
		LinearLayout adContainer = (LinearLayout) activity.findViewById(R.id.ads_container);
		adContainer.addView(new TitleBuilder().createTitle(activity, UNION));

		this.taobaoContainer = new LinearLayout(activity);
		adContainer.addView(taobaoContainer);
		this.ad = new TaoAds(activity);
		System.out.println(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,pid = "+pid);
		if(pid!=null && pid.trim().length()>0){
			System.out.println(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,pid = "+pid);
			this.ad.setPid(pid);
		}
		ad.setListener(this.adRequestListener);
		ad.setContainer(taobaoContainer);
		ad.requestAds();
	}
	/*************************** munion.taobao.com end ************************/

	class TaobaoAdRequestListener extends AbstractAdsExpertAdListener implements TaoAdsListener{
		public TaobaoAdRequestListener(Activity activity, String union){
			super(activity, union);
		}

		@Override
		public void onAdEvent(int event, String arg1) {
			// TODO Auto-generated method stub
			switch(event){
				case TaoAdsListener.EVENT_TYPE_GET_AD_SUCCESS:{
					System.out.println("taobaoContainer.getVisibility() = "+(taobaoContainer.getVisibility()==View.VISIBLE));
					this.succeed();
					AdHistory.saveAd(UNION, taobaoContainer);
					break;
				}
				case TaoAdsListener.EVENT_TYPE_GET_AD_FAIL:
					this.fail();
					break;
				case TaoAdsListener.EVENT_TYPE_AD_CLICKED:
					this.adClicked();
					break;
				case TaoAdsListener.EVENT_TYPE_START_REQUEST_AD:
					this.beforeAdRequest();
					break;
			}
		}

	}

}
